<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/3/20
 * Time: 16:13
 */
namespace app\home\controller;
use app\models\Bases;
use think\Config;

class Register extends Base{
    public function register(){
        try{
            $input = input('param.');
//先验证参数----------------------------------------------------
            foreach($input as  $k => $v){
                if(empty($v)){
                    throw new \LogicException('缺少参数',1);
                }
            }
//验证用户是否已经注册
            $userModel = new Bases('user');
            $where['username'] = ['=',$input['username']];
            $userInfo  = $userModel->find($where);
            if(!empty($userInfo)){
                throw new \LogicException('该手机号已被注册',1);
            }
            self::checkCode($input['code'],'register',$input['username']);  //验证验证码是否正确

            if((int)$input['validatePwd'] != (int)$input['password']){      //验证两次密码输入是否相同
                throw new \LogicException('两次密码不一致',1);
            }
            $params = [
                'username' => trim($input['username']),
                'nickname' => trim($input['nickname']),
                'password' => password_hash(trim($input['password']),1),
                'source'   => 1,
                'integral' => Config::get('integral.register'),
                'create_time' => time(),
                'status'   => 1,
            ];
            $install = $userModel->insertData($params);
            if(!$install){
                throw new \LogicException('注册失败',1);
            }
            return json(self::formatSuccessResult('success'));
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
    }
    public function valiCode($key,$code){
        if($_COOKIE[$key] != $code){
            return false;
        }
        return true;
    }
}